import { defineStore } from 'pinia'
import { LoginPost, UserInfoGet, CreateUserPost, LogoutGet } from "@/api/user";

export const useUserStore = defineStore('user', {
  state: () => ({
    user: {
      token: null,
      info: null
    } as any
  }),
  actions: {
    async login(account: string, password: string) {
      const { token } = await LoginPost(account, password) as any;
      this.user.token = token;
      await UserInfoGet()
    },
    async create(account: string, password: string) {
      await CreateUserPost(account, password)
    },
    async logout() {
      await LogoutGet()
      this.user.token = null
      this.user.info = null
    }
  },
  persist: {
    pick: ["user.token"]
  },
})